home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / MCASM.RAR / MC_ASM.EXE / WROX_ASM / CH12 / COMMON / KEYMOUSE.CPP < prev    next >
C/C++ Source or Header  |  1994-09-24  |  3KB  |  168 lines

  1. /************************************************************************/
  2. /* This program is   implementation of                      */
  3. /* mouse & keyboard dealing procedure described in keymouse.h        */
  4. /*    Kiselev Y. & E.Podvoysky from ^Z for WROX press book, 1994    */
  5. /************************************************************************/
  6.  
  7. #include <dos.h>
  8. #include <conio.h>
  9. #include "graph.h"
  10. #include "keymouse.h"
  11.  
  12. WORD readkeyword() {
  13. asm {
  14.         xor ax,ax
  15.         int 16h
  16. }
  17. }
  18.  
  19. BYTE keyboardstatus() {
  20. asm {
  21.         mov ah,2
  22.         int 16h
  23. }
  24. }
  25. /////////////////////////////////////////////////////////////////
  26. int num_of_buttom;
  27. int mousex,mousey,oldx,oldy;
  28. BOOL visible_mouse = FALSE;
  29. WORD buttons = 0;
  30. BYTE ppb = 3; // 0 for 640x480x16,320x200x256
  31.         // 3 for 640x480x256,800x600x256
  32.  
  33. BYTE getmousedata(int &x,int &y) {
  34. asm {
  35.     mov ax,3;
  36.     int 0x33;
  37.  
  38. }
  39. x = _CX; y = _DX;
  40. x >>= ppb;
  41. y >>= ppb;
  42. return(_BX);
  43. }
  44.  
  45. int initmouse() {
  46. asm { xor ax,ax; int 0x33;}
  47. if (_AX == 0xFFFF) {num_of_buttom = _BX; return 0;} else return -1;
  48. }
  49.  
  50. void hide_mouse() {
  51. asm { mov ax,2; int 0x33;}
  52. }
  53.  
  54. void show_mouse() {
  55. asm { mov ax,1; int 0x33;}
  56. }
  57.  
  58. void hide_mymouse() {
  59.     if (visible_mouse) {
  60.         setwritemode(XOR_PUT);
  61.         setcolor(255);
  62.         line(oldx - 10,oldy,oldx + 10, oldy);
  63.         line(oldx ,oldy - 10,oldx,oldy + 10);
  64.         setwritemode(NORMAL_PUT);
  65.     }
  66.     visible_mouse = FALSE;
  67. }
  68.  
  69. void show_mymouse() {
  70.     if (!visible_mouse) {
  71.         setwritemode(XOR_PUT);
  72.         setcolor(255);
  73.         line(mousex - 10,mousey,mousex + 10, mousey);
  74.         line(mousex ,mousey - 10,mousex,mousey + 10);
  75.         oldx = mousex; oldy = mousey;
  76.         setwritemode(NORMAL_PUT);
  77.     }
  78.     visible_mouse = TRUE;
  79. }
  80.  
  81. void move_mymouse() {
  82.     if ((mousex != oldx) || (mousey != oldy)) { hide_mymouse(); show_mymouse();}
  83. }
  84.  
  85. void setmousepos(int x,int y) {
  86. mousex = x; mousey = y;
  87. x <<= ppb;
  88. y <<= ppb;
  89. asm {
  90.     mov ax,4;
  91.     mov cx,x;
  92.     mov dx,y;
  93.     int 0x33
  94. }
  95. }
  96.  
  97. void setmouserange(int x1,int y1,int x2,int y2) {
  98. x1 <<= ppb;
  99. x2 <<= ppb;
  100. y1 <<= ppb;
  101. y2 <<= ppb;
  102. asm {
  103.     mov ax,7;
  104.     mov cx,x2;
  105.     mov dx,x1;
  106.     int 0x33;
  107.     mov ax,8;
  108.     mov cx,y2;
  109.     mov dx,y1;
  110.     int 0x33;
  111. }
  112. }
  113.  
  114. void setmousehandler(void far *handle,WORD mask) {
  115. asm {mov ax,12; les dx,handle; mov cx,mask; int 0x33}
  116. }
  117.  
  118. void far mousehandler() {
  119. asm {
  120.         push ds
  121.         mov ax,SEG mousex
  122.         mov ds,ax
  123.         mov ax,cx
  124.         mov cl,ppb
  125.         shr ax,cl
  126.         mov mousex,ax
  127.         mov ax,dx
  128.         shr ax,cl
  129.         mov mousey,ax
  130.         mov bh,0
  131.         mov buttons,bx
  132.         pop ds
  133. }
  134. }
  135.  
  136. void initmouse_easy() {
  137. initmouse();
  138. setmousehandler(mousehandler,0x007F);
  139. }
  140.  
  141. void getmousestate(int &x,int &y,WORD &button) {
  142. int i;
  143. button = buttons;
  144. while (buttons >= button) {
  145.     move_mymouse();
  146.     button = buttons;
  147.     for(i=0;i<50;i++);
  148. }
  149. while (buttons != 0) for(i=0;i<50;i++);
  150. x = mousex;
  151. y = mousey;
  152. }
  153.  
  154. void delay_key(long time) {
  155. for (; time > 0; time--) {
  156.     delay(1);
  157.     if (kbhit()) break;
  158. }
  159. }
  160.  
  161. void clearkey() { while (kbhit()) readkeyword();}
  162.  
  163. void delay_key1(long time) {
  164.     clearkey();
  165.     delay_key(time);
  166. }
  167.  
  168.